﻿/*****************************************************************************
 
 Use of Ext is under the Ext JS license, for more information, 
 please visit http://extjs.com/products/license.php page. 
  
 You should confirm that you have read, understood and accept ethical criteria listed. 
 If not, see <http://www.mvcnext.com/license/>.
  
 Version: 1.0.0.0
 Website: http://www.mvcnext.com/
 
 Copyright (c) Yoganand R. All rights reserved.
 For continued development and updates, visit http://yogsbreath.net
 
 ******************************************************************************/
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Collections;
using System.Web.UI;

namespace MVCnExt.Web.UI.Ext
{
    /// <summary>
    /// Provides a time input field with a time dropdown and automatic time validation.
    /// </summary>
    [Description("Provides a time input field with a time dropdown and automatic time validation.")]
    public class ExtTimeField : Field  
    {
        public override List<ScriptReference> GetScriptReferences()
        {
            return base.GetScriptReferences();
        }

        public override IList<ScriptDescriptor> GetScriptDescriptors()
        {
            IList<ScriptDescriptor> list = base.GetScriptDescriptors();

            ScriptControlDescriptorBase desc = (ScriptControlDescriptorBase)list[0];

            desc.Type = "Ext.form.TimeField";

            String[] properties = new String[]{
                "format", "increment", "maxText", "maxValue",  "minText", "minValue"            
            };

            Type component = typeof(ExtTimeField);

            foreach (String property in properties)
            {
                PropertyInfo i = component.GetProperty(property);

                if (i == null)
                    throw new InvalidOperationException(String.Format("Property not found, 'ExtTimeField.{0}'", property));

                Object value = i.GetValue(this, null);

                if (value == null)
                {
                    continue;
                }

                if (!(value is String))
                    desc.AddPropertyBase(property, value);
                else
                    desc.AddPropertyBase(property, String.Format("'{0}'", value));
            }                        

            return list;
        }

        #region Public Properties

        /// <summary>
        /// The default date format string which can be overriden for localization support. The format must be valid according to Date.parseDate (defaults to 'm/d/y').
        /// </summary>
        [DefaultValue("MM/dd/yy")]
        [Description("The default date format string which can be overriden for localization support. The format must be valid according to Date.parseDate (defaults to 'm/d/y').")]
        public String format { get; set; }

        /// <summary>
        /// The number of minutes between each time value in the list (defaults to 15).
        /// </summary>
        [DefaultValue(15)]
        [Description("The number of minutes between each time value in the list (defaults to 15).")]
        public int increment { get; set; }

        /// <summary>
        /// The error text to display when the time is after maxValue (defaults to 'The time in this field must be equal to or before {0}').
        /// </summary>
        [Description("The error text to display when the time is after maxValue (defaults to 'The time in this field must be equal to or before {0}').")]
        public String maxText { get; set; }

        /// <summary>
        /// The maximum allowed time. Can be either a Javascript date object or a string date in a valid format (defaults to null).
        /// </summary>
        [Description("The maximum allowed time. Can be either a Javascript date object or a string date in a valid format (defaults to null).")]
        public String maxValue { get; set; }

        /// <summary>
        /// The error text to display when the date in the cell is before minValue (defaults to 'The time in this field must be equal to or after {0}').
        /// </summary>
        [Description("The error text to display when the date in the cell is before minValue (defaults to 'The time in this field must be equal to or after {0}').")]
        public String minText { get; set; }

        /// <summary>
        /// The minimum allowed time. Can be either a Javascript date object or a string date in a valid format (defaults to null).
        /// </summary>
        [Description("The minimum allowed time. Can be either a Javascript date object or a string date in a valid format (defaults to null).")]
        public String minValue { get; set; }      
       
        #endregion
    }
}
